﻿namespace Zo.Extensions
{
    /// <summary>
    ///
    /// </summary>
    public static class DecimalExtensions
    {
        /// <summary>
        /// 保留小数。
        /// </summary>
        /// <param name="source">原数字。</param>
        /// <param name="number">保留的小数位数。</param>
        /// <returns></returns>
        public static decimal MathRound(this decimal source, int number = 0)
        {
            if (number <= 0)
                return (int)source;

            var pow = 10;

            for (var index = 0; index < number - 1; index++)
            {
                pow *= 10;
            }

            var target = ((int)(source * pow) + 0.0m) / pow;

            return target;
        }
    }
}